<template>
	<view :style="{ width: cardWidth, height: cardHeight }" class="goods-card">
		<!-- 商品图片 -->
		<image :src="goods.imageUrl" class="goods-image" mode="aspectFill"></image>
		<!-- 商品名称 -->
		<view class="goods-name">{{ goods.name }}</view>
		<!-- 供应商信息及位置按钮 -->
		<view class="supplier-info">
			<view class="supplier">{{ goods.supplier }}</view>
			<view class="location-button" @click="handleLocation">
				<image src="/static/icon/location.png"></image>
			</view>
		</view>
		<!-- 商品价格 -->
		<view class="goods-price">￥{{ goods.price }}</view>
		<!-- 按钮组 -->
		<view class="button-group">
			<view class="buy-button" @click="handleBuy">立即购买</view>
			<view class="add-to-cart-button" @click="handleAddToCart">添加到购物车</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';

	// 定义组件的props，接收商品数据、卡片宽度和卡片高度
	const props = defineProps({
		goods: {
			type: Object,
			required: true
		},
		cardWidth: {
			type: String,
			default: '100%'
		},
		cardHeight: {
			type: String,
			default: 'auto'
		}
	});

	// 解构props中的数据
	const {
		goods,
		cardWidth,
		cardHeight
	} = props;

	// 立即购买按钮的点击处理函数
	const handleBuy = () => {
		// 这里可以添加购买逻辑，比如调用接口下单等
		console.log(`购买商品：${goods.name}`);
	};

	// 添加到购物车按钮的点击处理函数
	const handleAddToCart = () => {
		// 这里可以添加添加到购物车的逻辑，比如调用接口将商品添加到购物车
		console.log(`将商品 ${goods.name} 添加到购物车`);
	};

	// 位置按钮的点击处理函数
	const handleLocation = () => {
		// 这里可以添加获取位置信息的逻辑，比如调用地图接口等
		console.log(`查看商品 ${goods.name} 供应商的位置`);
	};
</script>

<style scoped lang="scss">
	.goods-card {
		box-sizing: border-box;
		border: 1px solid #ccc;
		border-radius: 8px;
		padding: 16px;
		margin-bottom: 16px;
		background-color: #fff;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.goods-image {
		width: 100%;
		height: 100px;
		object-fit: cover;
		margin-bottom: 12px;
		border-radius: 4px;
	}

	.goods-name {
		font-size: 18px;
		font-weight: bold;
		margin-bottom: 8px;
	}

	.supplier-info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 12px;
	}

	.supplier {
		font-size: 14px;
		color: #666;
	}

	.location-button {
		cursor: pointer;

		image {
			width: 20px;
			height: 20px;
		}
	}

	.goods-price {
		font-size: 16px;
		color: #f00;
		margin-bottom: 12px;
	}

	.button-group {
		display: flex;
		justify-content: space-between;
	}

	.buy-button {
		padding: 10px 20px;
		background-color: #1aad19;
		color: #fff;
		border-radius: 4px;
		text-align: center;
		cursor: pointer;
	}

	.add-to-cart-button {
		padding: 10px 20px;
		background-color: #007AFF;
		color: #fff;
		border-radius: 4px;
		text-align: center;
		cursor: pointer;
	}
</style>